package com.digital.smartgrow.service.sysinfo.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.smartgrow.common.util.BeanCopyUtil;
import com.digital.smartgrow.dao.sysinfo.DictionaryMapper;
import com.digital.smartgrow.dto.datacenter.DictionaryPageDTO;
import com.digital.smartgrow.entity.sysinfo.Dictionary;
import com.digital.smartgrow.entity.sysinfo.DictionaryItem;
import com.digital.smartgrow.service.sysinfo.DictionaryItemService;
import com.digital.smartgrow.service.sysinfo.DictionaryService;
import com.digital.smartgrow.vo.dict.DictItemVO;
import com.digital.smartgrow.vo.dict.DictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 字典 服务实现类
 * </p>
 *
 * @author xiewb
 * @since 2022-04-23
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {
    @Autowired
    private DictionaryItemService dictionaryItemService;

    @Override
    public Object getListByPage(DictionaryPageDTO pageDTO) {
        Page<Dictionary> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(pageDTO.getStatus() != null, "status", pageDTO.getStatus());
        if (StringUtils.isNotBlank(pageDTO.getDictNumber())||pageDTO.getDictNumber() != null) {
            queryWrapper
                    .and(
                            wrapper ->
                                    wrapper.like("dictName", pageDTO.getDictNumber()).
                                            or().like("dictNumber", pageDTO.getDictNumber())

                    );
        }
        Page<Dictionary> page1 = this.page(page, queryWrapper);

        List<Dictionary> records = page1.getRecords();
        List<DictVo> dictVoList = new ArrayList<>();
        for (Dictionary dict : records) {
            String dictNumber = dict.getDictNumber();
            List<DictionaryItem> itemList = dictionaryItemService.lambdaQuery()
                    .eq(DictionaryItem::getStatus, 1)
                    .eq(DictionaryItem::getDeleted, 0)
                    .in(StringUtils.isNotBlank(dictNumber), DictionaryItem::getDictNumber, dictNumber)
                    .list();
            List<DictItemVO> itemVOS = new ArrayList<>();
            for (DictionaryItem item : itemList) {
                itemVOS.add(new DictItemVO(item));
            }

            dictVoList.add(new DictVo(dict, itemVOS));

        }
        //return dictVoList;
        Page<DictVo> page2 = new Page<>();
        page2.setRecords(dictVoList);
        page2.setTotal(page1.getTotal());
        page2.setSize(page1.getSize());
        page2.setCurrent(page1.getCurrent());

        page2.setOrders(page1.getOrders());
        page2.setCountId(page1.getCountId());
        page2.setMaxLimit(page1.getMaxLimit());

        return page2;
    }
    //@Override
    public Object getListByPageBak(DictionaryPageDTO pageDTO) {
        Page<Dictionary> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(pageDTO.getStatus() != null, "status", pageDTO.getStatus());
        if (StringUtils.isNotBlank(pageDTO.getDictNumber())||pageDTO.getDictNumber() != null) {
            queryWrapper
                    .and(
                            wrapper ->
                                    wrapper.like("dictName", pageDTO.getDictNumber()).
                                            or().like("dictNumber", pageDTO.getDictNumber())

                    );
        }
        Page<Dictionary> page1 = this.page(page, queryWrapper);

        List<Dictionary> records = page1.getRecords();

        List<DictVo> dictVoList = BeanCopyUtil.copyListProperties(records,DictVo::new);
        Page<DictVo> page2 = new Page<>();
        page2.setRecords(dictVoList);
        page2.setTotal(page1.getTotal());
        page2.setSize(page1.getSize());
        page2.setCurrent(page1.getCurrent());
        page2.setOrders(page1.getOrders());
        page2.setCountId(page1.getCountId());
        page2.setMaxLimit(page1.getMaxLimit());
//        List<Dictionary> records = page1.getRecords();
//        List<DictVo> dictVoList = new ArrayList<>();
//        for (Dictionary dict : records) {
//            String dictNumber = dict.getDictNumber();
//            List<DictionaryItem> itemList = dictionaryItemService.lambdaQuery()
//                    .eq(DictionaryItem::getStatus, 1)
//                    .eq(DictionaryItem::getDeleted, 0)
//                    .in(StringUtils.isNotBlank(dictNumber), DictionaryItem::getDictNumber, dictNumber)
//                    .list();
//            List<DictItemVO> itemVOS = new ArrayList<>();
//            for (DictionaryItem item : itemList) {
//                itemVOS.add(new DictItemVO(item));
//            }
//
//            dictVoList.add(new DictVo(dict, itemVOS));
//
//        }
//        //return dictVoList;
//        Page<DictVo> page2 = new Page<>();
//        page2.setRecords(dictVoList);
//        page2.setTotal(page1.getTotal());
//        page2.setSize(page1.getSize());
//        page2.setCurrent(page1.getCurrent());
//
//        page2.setOrders(page1.getOrders());
//        page2.setCountId(page1.getCountId());
//        page2.setMaxLimit(page1.getMaxLimit());

        return page2;
    }
    @Override
    public Object getInfo(Integer id) {
        Dictionary dictionary = this.getById(id);
        String dictNumber = dictionary.getDictNumber();
        List<DictionaryItem> itemList = dictionaryItemService.lambdaQuery()
                .eq(DictionaryItem::getStatus, 1)
                .eq(DictionaryItem::getDeleted, 0)
                .in(StringUtils.isNotBlank(dictNumber), DictionaryItem::getDictNumber, dictNumber)
                .list();
        List<DictItemVO> itemVOS = new ArrayList<>();
        for (DictionaryItem item : itemList) {
            itemVOS.add(new DictItemVO(item));
        }
        return new DictVo(dictionary, itemVOS);
    }

    @Override
    public Object removeAllById(Integer id) {
        Dictionary dictionary = this.getById(id);
        String dictNumber = dictionary.getDictNumber();
        List<DictionaryItem> itemList = dictionaryItemService.lambdaQuery()
                .eq(DictionaryItem::getStatus, 1)
                .eq(DictionaryItem::getDeleted, 0)
                .in(StringUtils.isNotBlank(dictNumber), DictionaryItem::getDictNumber, dictNumber)
                .list();
        //dictionaryItemService.remove((Wrapper<DictionaryItem>) itemList);
        for (DictionaryItem item : itemList) {
            dictionaryItemService.removeById(item.getId());
        }
        return this.removeById(id);
    }

    @Override
    public Object saveAll(DictVo dict) throws Exception {
        Dictionary dictionary = new Dictionary();
        //转换赋值
        dictionary.setDictName(dict.getDictName());
        dictionary.setDictNumber(dict.getDictNumber());
        dictionary.setId(dict.getId());
        dictionary.setStatus(dict.getStatus());
        dictionary.setIssys(dict.getIssys());
        boolean save = this.save(dictionary);
        List<DictItemVO> items = dict.getItems();
        for (DictItemVO itemVO : items) {
            DictionaryItem dictionaryItem = new DictionaryItem();
            //转换赋值
            dictionaryItem.setDictNumber(dict.getDictNumber());
            dictionaryItem.setItemName(itemVO.getItemName());
            dictionaryItem.setItemNumber(itemVO.getItemNumber());
            dictionaryItemService.save(dictionaryItem);
        }
        return dictionary.getId();
    }

    @Override
    public Object updateAll(DictVo dict) {
        //先删除子项
        String dictNumber = dict.getDictNumber();
        List<DictionaryItem> itemList = dictionaryItemService.lambdaQuery()
                .eq(DictionaryItem::getStatus, 1)
                .eq(DictionaryItem::getDeleted, 0)
                .in(StringUtils.isNotBlank(dictNumber), DictionaryItem::getDictNumber, dictNumber)
                .list();
        //dictionaryItemService.remove((Wrapper<DictionaryItem>) itemList);
        for (DictionaryItem item : itemList) {
            dictionaryItemService.removeById(item.getId());
        }
        //更新主表
        Dictionary dictionary = this.getById(dict.getId());
        dictionary.setDictName(dict.getDictName());
        dictionary.setDictNumber(dict.getDictNumber());
        dictionary.setStatus(dict.getStatus());
        dictionary.setIssys(dict.getIssys());
        this.updateById(dictionary);
        //新增子项
        List<DictItemVO> items = dict.getItems();
        for (DictItemVO itemVO : items) {
            DictionaryItem dictionaryItem = new DictionaryItem();
            //转换赋值
            dictionaryItem.setDictNumber(dict.getDictNumber());
            dictionaryItem.setItemName(itemVO.getItemName());
            dictionaryItem.setItemNumber(itemVO.getItemNumber());
            dictionaryItemService.save(dictionaryItem);
        }
        return dict;
    }

    /**
     * 根据ID及关键字查询code, 模糊查询
     *
     * @param id      字典ID
     * @param keyword 关键字
     * @return
     */
    @Override
    public String selectBykey(Integer id, String keyword) {
//        List<DictionaryItem> itemList = dictionaryItemService.lambdaQuery()
//                .eq(DictionaryItem::getStatus, 1)
//                .eq(DictionaryItem::getDeleted, 0)
//                .in(StringUtils.isNotBlank(dictNumber), DictionaryItem::getDictNumber, dictNumber)
//                .list();
        Dictionary byId = this.getById(id);
        String dictNumber = byId.getDictNumber();

        //List<DictionaryItem> list = dictionaryItemService.lambdaQuery().eq(DictionaryItem::getDictNumber, dictNumber).list();
        List<DictionaryItem> list = dictionaryItemService.lambdaQuery().eq(DictionaryItem::getDictNumber, dictNumber).like(DictionaryItem::getItemName, keyword).list();
        if(list.size()==0){
            return null;
        }
        String itemNumber = list.get(0).getItemNumber();

        return itemNumber;
    }

    @Override
    public Object select(String dictNumber) {
        List<DictionaryItem> itemList = dictionaryItemService.lambdaQuery()
                .eq(DictionaryItem::getStatus, 1)
                .eq(DictionaryItem::getDeleted, 0)
                .in(StringUtils.isNotBlank(dictNumber), DictionaryItem::getDictNumber, dictNumber)
                .list();
        List<DictItemVO> items = new ArrayList<>();
        for (DictionaryItem item : itemList) {
            items.add(new DictItemVO(item));
        }
        return items;
    }


}
